package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.ShopTrolleyDao;
import cn.ym.mall.entity.ShopTrolley;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 购物车 DAO 实现类
 */
public class ShopTrolleyDaoImpl implements ShopTrolleyDao {
    QueryRunner queryRunner = new QueryRunner();

    /**
     * 新增购物车
     *
     * @param shopTrolley 添加的购物车
     * @return 返回成功的条数
     */
    @Override
    public Integer addShopTrolley(ShopTrolley shopTrolley) throws SQLException {
        String sql = "insert into ym_shoptrolley(uid, gid, stnum, stprice,stjoinTime) values (?,?,?,?,?);";
        Object[] params = new Object[]{shopTrolley.getUid(), shopTrolley.getGid(), shopTrolley.getStnum(), shopTrolley.getStprice(), shopTrolley.getStjoinTime()};
        return queryRunner.update(DruidUtils.getConnection(), sql, params);
    }

    /**
     * 根据购物车编号删除购物车
     *
     * @param stid 购物车
     * @return 返回删除条数
     */
    @Override
    public Integer deleteByStid(Integer stid) throws SQLException {
        int num = 0;
        try {
            String sql = "delete from ym_shoptrolley where stid = ?;";
            num = queryRunner.update(DruidUtils.getConnection(), sql, stid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询所有购物车信息
     *
     * @return 购物车信息
     * @throws SQLException
     */
    @Override
    public List<ShopTrolley> getAllShopTrolley() throws SQLException {
        List<ShopTrolley> shopTrolleyList = null;
        String sql = "select * from ym_shoptrolley order by stjoinTime desc;";
        try {
            shopTrolleyList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<ShopTrolley>(ShopTrolley.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return shopTrolleyList;
    }

    /**
     * 根据购物车编号查询购物车信息
     *
     * @param stid
     * @return
     */
    @Override
    public ShopTrolley getShopTrolleyByStid(Integer stid) throws SQLException {
        String sql = "select * from ym_shoptrolley where stid = ?;";
        return queryRunner.query(DruidUtils.getConnection(), sql, new BeanHandler<ShopTrolley>(ShopTrolley.class), stid);
    }

    /**
     * 根据商品编号统计购物车商品数量
     *
     * @param gid 商品编号
     * @return 返回商品数量
     * @throws SQLException
     */
    @Override
    public long getCountByGid(Integer gid) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_shoptrolley where gid = ?;";
            count = queryRunner.query(DruidUtils.getConnection(), sql, new ScalarHandler<>(), gid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 修改购物车
     *
     * @param shopTrolley 购物车
     * @return 返回更新成功的条数
     */
    @Override
    public Integer updateShopTrolley(ShopTrolley shopTrolley) throws SQLException {
        int num = 0;

        try {
            String sql = "update ym_shoptrolley set stnum = ? where stid = ?;";
            num = queryRunner.update(DruidUtils.getConnection(), sql, shopTrolley.getStnum(), shopTrolley.getStid());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }
}
